package hah;

import com.sun.tools.classfile.Opcode;

import java.util.HashSet;

public class lengthOfLongestSubstrin {
    public int lengthOfLongestSubstrin(String s){
        //无重复字符的最长字串   -----滑动窗口
        if (s==null||s.length()==0){
            return 0;
        }
        int len=s.length();
        int l=0;
        int r=0;
        int res=0;
        HashSet<Character> set=new HashSet<>();
        set.add(s.charAt(r));
        while ((l<len)){
            while(r+1<len&&!set.contains(s.charAt(r+1))){
                set.add(s.charAt(r+1));
                ++r;
            }
            res=Math.max(res,r-l+1);
            set.remove(s.charAt(l));
            ++l;
        }
        return res;

    }
}
